An Exploratory Data Analysis of National Bridge Inventory

Connection to Database:


In [139]:
import pymongo
from pymongo import MongoClient
file = open("dbconnect", 'r')
dbConnectionString = str(file.read()).strip()
client = MongoClient(dbConnectionString)
db = client.bridge
collection = db["nbi"]

Bridge Records in DB:


In [140]:
print("Bridge Records in DB: ", collection.count())


Bridge Records in DB:  16557806

Total number of Bridge records from year 1992 to year 2016:


In [ ]:
print('YEAR '+':'+' TOTAL NUMBER OF BRIDGES')
for i in collection.distinct("year"):
    print(i, ": ", collection.find({"year":i}).count())


YEAR : TOTAL NUMBER OF BRIDGES
1992 :  663064

In [52]:
import pandas as pd
columns =["stateCode","deckWidthOutToOut","structureLength","averageDailyTraffic"]
nbi = pd.DataFrame(columns=columns)

#statecodes = ["31","04"]

statecodes = ['25',
          '04', 
          '08',
          '38', 
          '09', 
          '19', 
          '26', 
          '48',
          '35',
          '17', 
          '51',
          '23',
          '16',
          '36',
          '56',
          '29',
          '39',
          '28', 
          '11',
          '21', 
          '18',
          '06',
          '47', 
          '12',
          '24',
          '34', 
          '46',
          '13',
          '55',
          '30',
          '54',
          '15', 
          '32', 
          '37',
          '10', 
          '33', 
          '44', 
          '50', 
          '42', 
          '05', 
          '20', 
          '45',
          '22',
          '40', 
          '72', 
          '41',  
          '21', 
          '53', 
          '01', 
          '31',
          '02', 
          '49'
             ]

for i in statecodes:
    result = collection.find({"year":2016,"stateCode":i})
    temp = pd.DataFrame(list(result))
    try:
        temp = temp[["stateCode","deckWidthOutToOut","structureLength","averageDailyTraffic"]]
    except:
        print(i)
        pass
    df_new = pd.concat([nbi, temp])
    nbi = df_new


22

In [53]:
nbi.count()


Out[53]:
stateCode              602277
deckWidthOutToOut      602277
structureLength        602277
averageDailyTraffic    602277
dtype: int64

In [63]:
nbi.head()


Out[63]:
stateCode deckWidthOutToOut structureLength averageDailyTraffic
0 25 7.9 10.4 30
1 25 11.0 94.5 500
2 25 14.7 8.8 6595
3 25 12.2 7.0 10000
4 25 15.0 23.7 11309

In [70]:
stateName = {'25':'MASSACHUSETTS',
             '04':'ARIZONA', 
             '08':'COLORADO',
             '38':'NORTH DAKOTA', 
             '09':'CONNECTICUT', 
             '19':'IOWA', 
             '26':'MICHIGAN', 
             '48':'TEXAS',
             '35':'NEW MEXICO',
             '17':'ILLINOIS', 
             '51':'VIRGINIA',
             '23':'MAINE',
             '16':'IDAHO',
             '36':'NEW YORK',
             '56':'WYOMING',
             '29':'MISSOURI',
             '39':'OHIO',
             '28':'MISSISSIPI', 
             '11':'DISTRICT OF COLOMBIA',
             '21':'KENTUCKY', 
             '18':'INDIANA',
             '06':'CALIFORNIA',
             '47':'TENNESSEE', 
             '12':'FLORIDA',
             '24':'MARYLAND',
             '34':'NEW JERSEY', 
             '46':'SOUTH DAKOTA',
             '13':'GEORGIA',
             '55':'WISCONSIN',
             '30':'MONTANA',
             '54':'WEST VIGINIA',
             '15':'HAWAII', 
             '32':'NEVADA', 
             '37':'NORTH CAROLINA',
             '10':'DELAWARE', 
             '33':'NEW HAMPSHIRE', 
             '44':'RHODE ISLAND',
             '50':'VERMONT', 
             '42':'PENNSYLVANIA', 
             '05':'ARKANSAS', 
             '20':'KANSAS', 
             '45':'SOUTH CAROLINA',
             '22':'LOUISIANA',
             '40':'OKLAHOMA', 
             '72':'PUERTO RICO', 
             '41':'OREGON',
             '21':'MINNESOTA', 
             '53':'WASHINGTON', 
             '01':'ALABAMA', 
             '31':'NEBRASKA',
             '02':'ALASKA', 
             '49':'UTAH'
               }
nbi['stateName'] =  nbi['stateCode'].map(stateName)

In [83]:
nbi['deckArea']= nbi['deckWidthOutToOut'] * nbi['structureLength']
nbi_summary = nbi[['stateName','deckWidthOutToOut','structureLength','deckArea','averageDailyTraffic']]
nbi_summary.groupby(['stateName']).agg({'stateName':'count',
                                'deckArea':'sum',
                                'averageDailyTraffic':'sum'})
#nbi_summary.columns=['Index','States','Total Valid Bridge','Total Deck Area','Total Average Daily Traffic']


Out[83]:
stateName deckArea averageDailyTraffic
stateName
ALABAMA 16098 9238040.53 76613580
ALASKA 1488 710703.90 3357512
ARIZONA 8154 5055845.20 97256999
ARKANSAS 12871 6381829.40 49636105
CALIFORNIA 25431 29478067.98 667205896
COLORADO 8680 4926475.88 68595652
CONNECTICUT 4214 3265916.52 78275293
DELAWARE 877 978596.38 11361551
DISTRICT OF COLOMBIA 245 568827.33 7660611
FLORIDA 12313 16759415.81 213337215
GEORGIA 14835 9404058.27 135068507
HAWAII 1132 1319916.61 27400179
IDAHO 4445 1723198.53 11566802
ILLINOIS 26704 13004416.75 133709726
INDIANA 19245 7997458.47 97813523
IOWA 24184 8248470.40 34260303
KANSAS 25013 8192548.56 46401837
MAINE 2450 1217855.97 11420274
MARYLAND 5321 5155225.32 116163051
MASSACHUSETTS 5171 4055694.07 114458271
MICHIGAN 11156 6357402.03 93784086
MINNESOTA 28530 12107278.70 128664746
MISSISSIPI 17068 9093623.83 43837769
MISSOURI 24468 10361960.26 86300007
MONTANA 5276 2000703.82 10505792
NEBRASKA 15334 3969666.40 22562432
NEVADA 1933 1595543.65 32174713
NEW HAMPSHIRE 2486 1120425.98 16910083
NEW JERSEY 6730 6878062.72 160237725
NEW MEXICO 3973 1728319.62 26782628
NEW YORK 17462 12869629.22 169682377
NORTH CAROLINA 18099 9221997.64 112602177
NORTH DAKOTA 4400 1232040.30 4423736
OHIO 28284 13317571.18 177117858
OKLAHOMA 23053 8350748.75 70967585
OREGON 8118 5017158.18 56756580
PENNSYLVANIA 22791 12377122.98 163719521
PUERTO RICO 2205 1973541.98 38053215
RHODE ISLAND 772 786182.40 15845827
SOUTH CAROLINA 9358 6800337.44 46154561
SOUTH DAKOTA 5849 1664936.80 7112750
TENNESSEE 20123 9659962.24 156431834
TEXAS 53488 47253738.73 543042520
UTAH 3039 1922455.87 63527662
VERMONT 2766 853119.53 6841770
VIRGINIA 13892 9620708.82 122500324
WASHINGTON 8178 6890269.73 66350712
WEST VIGINIA 7217 3805059.64 24843795
WISCONSIN 14230 6706707.15 78666403
WYOMING 3128 1269968.77 7350946

In [81]:
nbi_summary.head()


Out[81]:
Index States Total Valid Bridge Total Deck Area Total Average Daily Traffic
0 MASSACHUSETTS 7.9 10.4 82.16 30
1 MASSACHUSETTS 11.0 94.5 1039.50 500
2 MASSACHUSETTS 14.7 8.8 129.36 6595
3 MASSACHUSETTS 12.2 7.0 85.40 10000
4 MASSACHUSETTS 15.0 23.7 355.50 11309

In [74]:
nbi.groupby(['stateName'])['stateName'].count()


Out[74]:
stateName
ALABAMA                 16098
ALASKA                   1488
ARIZONA                  8154
ARKANSAS                12871
CALIFORNIA              25431
COLORADO                 8680
CONNECTICUT              4214
DELAWARE                  877
DISTRICT OF COLOMBIA      245
FLORIDA                 12313
GEORGIA                 14835
HAWAII                   1132
IDAHO                    4445
ILLINOIS                26704
INDIANA                 19245
IOWA                    24184
KANSAS                  25013
MAINE                    2450
MARYLAND                 5321
MASSACHUSETTS            5171
MICHIGAN                11156
MINNESOTA               28530
MISSISSIPI              17068
MISSOURI                24468
MONTANA                  5276
NEBRASKA                15334
NEVADA                   1933
NEW HAMPSHIRE            2486
NEW JERSEY               6730
NEW MEXICO               3973
NEW YORK                17462
NORTH CAROLINA          18099
NORTH DAKOTA             4400
OHIO                    28284
OKLAHOMA                23053
OREGON                   8118
PENNSYLVANIA            22791
PUERTO RICO              2205
RHODE ISLAND              772
SOUTH CAROLINA           9358
SOUTH DAKOTA             5849
TENNESSEE               20123
TEXAS                   53488
UTAH                     3039
VERMONT                  2766
VIRGINIA                13892
WASHINGTON               8178
WEST VIGINIA             7217
WISCONSIN               14230
WYOMING                  3128
Name: stateName, dtype: int64

In [110]:
import pandas as pd
columns =["maintenanceReponsibility","deckWidthOutToOut","structureLength","averageDailyTraffic"]
nbi2 = pd.DataFrame(columns=columns)

#statecodes = ["31","04"]

statecodes = ['25',
          '04', 
          '08',
          '38', 
          '09', 
          '19', 
          '26', 
          '48',
          '35',
          '17', 
          '51',
          '23',
          '16',
          '36',
          '56',
          '29',
          '39',
          '28', 
          '11',
          '21', 
          '18',
          '06',
          '47', 
          '12',
          '24',
          '34', 
          '46',
          '13',
          '55',
          '30',
          '54',
          '15', 
          '32', 
          '37',
          '10', 
          '33', 
          '44', 
          '50', 
          '42', 
          '05', 
          '20', 
          '45',
          '22',
          '40', 
          '72', 
          '41',  
          '21', 
          '53', 
          '01', 
          '31',
          '02', 
          '49'
             ]

for i in statecodes:
    result = collection.find({"year":2016,"stateCode":i})
    temp2 = pd.DataFrame(list(result))
    try:
        temp2 = temp2[["maintenanceReponsibility","stateCode","deckWidthOutToOut","structureLength","averageDailyTraffic"]]
    except:
        print(i)
        pass
    df_new2 = pd.concat([nbi2, temp2])
    nbi2 = df_new2


22

In [111]:
nbi2.head()


Out[111]:
averageDailyTraffic deckWidthOutToOut maintenanceReponsibility stateCode structureLength
0 30 7.9 63 25 10.4
1 500 11.0 66 25 94.5
2 6595 14.7 3 25 8.8
3 10000 12.2 3 25 7.0
4 11309 15.0 1 25 23.7

In [112]:
maintenanceReponsibility ={ -1 : 'NA',
                      1 : 'State Highway Agency',
                      21: 'Other State Agency',
                      4 : 'City or Municipal Highway Agency',
                      80: 'Unknown',
                      66: 'National Park Service',
                      2 : 'County Highway Agency',
                      60: 'Other Federal Agencies (not listest below)',
                      64: 'U.S forest Services' ,
                      68: 'Bureau of Land Management',
                      26: 'Private (other than railroad)',
                      62: 'Bureau of Indian Affairs', 
                      3 : 'Town or Township Highway Agency',
                      25: 'Other Local Agencies',
                      11: 'State Park, Forest or Reservation Agency',
                      63: 'Bureau of Fish and Wildlife',
                      27: 'Railroad',
                      74: 'Army',
                      70: 'Corps of Engineers (Civil)',
                      72: 'Air Force',
                      61: 'Indian Tribal Agency',
                      71: 'Corps of Engineers (Military)',
                      69: 'Bureau of Reclamation',
                      67: 'Tennesssee Valley Authority',
                      32: 'Local Toll Authority',
                      12: 'Local Park, Forest or Reservation Agency',
                      31: 'State Toll Authority',
                      73: 'Navy / Marines',
                      75: 'NASA',
                      76: 'Metropolitian Washington Airports Service'
}
'''
for i in nbi2['maintenanceReponsibility']:
    i = str(i)
    nbi2['Maintenance Responsibility'] = maintenanceReponsibility[i]
'''    
nbi2['Maintenance Reponsibility'] =  nbi2['maintenanceReponsibility'].map(maintenanceReponsibility)

In [113]:
nbi2.head()


Out[113]:
averageDailyTraffic deckWidthOutToOut maintenanceReponsibility stateCode structureLength Maintenance Reponsibility
0 30 7.9 63 25 10.4 Bureau of Fish and Wildlife
1 500 11.0 66 25 94.5 National Park Service
2 6595 14.7 3 25 8.8 Town or Township Highway Agency
3 10000 12.2 3 25 7.0 Town or Township Highway Agency
4 11309 15.0 1 25 23.7 State Highway Agency

In [114]:
nbi2['deckArea']= nbi2['deckWidthOutToOut'] * nbi2['structureLength']

In [115]:
nbi2.head()


Out[115]:
averageDailyTraffic deckWidthOutToOut maintenanceReponsibility stateCode structureLength Maintenance Reponsibility deckArea
0 30 7.9 63 25 10.4 Bureau of Fish and Wildlife 82.16
1 500 11.0 66 25 94.5 National Park Service 1039.50
2 6595 14.7 3 25 8.8 Town or Township Highway Agency 129.36
3 10000 12.2 3 25 7.0 Town or Township Highway Agency 85.40
4 11309 15.0 1 25 23.7 State Highway Agency 355.50

In [120]:
nbi2_maintenance=nbi2.groupby(['Maintenance Reponsibility']).agg({'Maintenance Reponsibility:'count',
                                'deckArea':'sum',
                                'averageDailyTraffic':'sum'})

In [128]:
mant_valid_bridges = nbi2_maintenance['Maintenance Reponsibility'].sum()
mant_sum_deck = nbi2_maintenance['deckArea'].sum()
mant_sum_adt = nbi2_maintenance['averageDailyTraffic'].sum()

In [137]:
nbi2_maintenance['Percent Valid Bridge']= (nbi2_maintenance['Maintenance Reponsibility']/mant_valid_bridges)*100
nbi2_maintenance['Percent Deck Area']= (nbi2_maintenance['deckArea'] / mant_sum_deck)*100
nbi2_maintenance['Percent ADT']= (nbi2_maintenance['averageDailyTraffic'] / mant_sum_adt)*100

In [138]:
nbi2_maintenance


Out[138]:
Maintenance Reponsibility deckArea averageDailyTraffic Percent Valid Bridge Percent Deck Area Percent ADT
Maintenance Reponsibility
Air Force 268 1.167584e+05 433613 0.044498 0.032937 0.009519
Army 977 2.215070e+05 694736 0.162218 0.062486 0.015251
Bureau of Fish and Wildlife 274 3.676514e+04 24972 0.045494 0.010371 0.000548
Bureau of Indian Affairs 930 2.437195e+05 546836 0.154414 0.068752 0.012004
Bureau of Land Management 481 7.953658e+04 20686 0.079864 0.022437 0.000454
Bureau of Reclamation 146 4.004325e+04 61985 0.024241 0.011296 0.001361
City or Municipal Highway Agency 44433 2.514869e+07 301639643 7.377502 7.094354 6.621708
Corps of Engineers (Civil) 240 3.667470e+05 428352 0.039849 0.103458 0.009403
County Highway Agency 224108 4.515230e+07 215368300 37.210121 12.737299 4.727846
Indian Tribal Agency 39 3.607570e+03 3880 0.006475 0.001018 0.000085
Local Park, Forest or Reservation Agency 55 1.220126e+04 115480 0.009132 0.003442 0.002535
Local Toll Authority 1153 4.865998e+06 26856281 0.191440 1.372680 0.589559
Metropolitian Washington Airports Service 51 4.687062e+04 776200 0.008468 0.013222 0.017039
NA 23 1.360123e+04 90992 0.003819 0.003837 0.001997
NASA 6 2.086913e+04 20428 0.000996 0.005887 0.000448
National Park Service 1316 5.937345e+05 4161021 0.218504 0.167490 0.091344
Navy / Marines 209 1.465305e+05 307104 0.034702 0.041336 0.006742
Other Federal Agencies (not listest below) 14 1.436289e+04 63484 0.002325 0.004052 0.001394
Other Local Agencies 1514 7.316032e+05 4328259 0.251379 0.206383 0.095016
Other State Agency 755 6.512740e+05 4252465 0.125358 0.183722 0.093352
Private (other than railroad) 757 1.060094e+06 11324586 0.125690 0.299049 0.248602
Railroad 517 2.310299e+05 1988168 0.085841 0.065173 0.043645
State Highway Agency 283615 2.559670e+08 3758054015 47.090458 72.207349 82.498225
State Park, Forest or Reservation Agency 981 1.764977e+05 496985 0.162882 0.049789 0.010910
State Toll Authority 7731 1.383917e+07 204433233 1.283629 3.903980 4.487796
Tennesssee Valley Authority 38 1.089069e+05 115129 0.006309 0.030722 0.002527
Town or Township Highway Agency 26637 4.006227e+06 18420027 4.422716 1.130142 0.404363
U.S forest Services 4989 5.762751e+05 260387 0.828356 0.162565 0.005716
Unknown 20 1.691349e+04 27774 0.003321 0.004771 0.000610